<template>
  <div class="app-container">
    <div class="container">
      <!--      标题-->
      <el-row>
        <el-col :span="24" :xs="24">
          <div class="title">站点管理<i class="el-icon-caret-right"></i>站点列表</div>
        </el-col>
      </el-row>
      <!-- 搜索 -->
      <el-row style="margin-left: 20px;">
        <el-col :span="24" :xs="24">
          <Search
              :search-data="searchData"
              :search-params="queryParams"
              @change="searchParamsUpdate"
          >
          </Search>
        </el-col>
      </el-row>
      <!--    增加-->
      <el-row :gutter="10" style="margin-bottom: 15px;margin-left: 17px;">
        <el-col :span="1.5">
          <el-button
              style="border: 1px solid rgba(0,231,231,0.4);background: rgba(0,231,231,0.051);color: #ffffff;"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
          >新增</el-button>
        </el-col>
        <el-col :span="1">
          <right-toolbar :showSearch.sync="showSearch" :columns="columns"></right-toolbar>
        </el-col>
      </el-row>
      <!--      表格-->
      <el-table
          :data="tableData"
          height="64vh"
          style="width: 100%;background: #002127;"
          :cell-style="{backgroundColor: '#002127',color: '#ffffff',}"
          :header-cell-style="{ backgroundColor: '#00484D',textAlign: 'center',fontWeight: 'bold',color: '#ffffff',borderBottom: '1px solid #00484D' }">
        <el-table-column prop="name" label="站点名称" align="center" v-if="columns[0].visible"></el-table-column>
        <el-table-column prop="longitude" label="经度" align="center" v-if="columns[1].visible"></el-table-column>
        <el-table-column prop="latitude" label="纬度" align="center" v-if="columns[2].visible"></el-table-column>
        <el-table-column prop="address" label="设备地址" align="center" v-if="columns[3].visible"></el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center" v-if="columns[4].visible" show-overflow-tooltip sortable></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" align="center" v-if="columns[5].visible" show-overflow-tooltip sortable></el-table-column>
        <el-table-column label="操作" align="center" width="250">
          <template slot-scope="scope">
            <el-button
                style="border: 1px solid rgba(0,231,231,0.4);background: rgba(0,231,231,0.051);color: #ffffff;"
                size="mini"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button
                style="border: 1px solid rgba(0,231,231,0.4);background: rgba(0,231,231,0.051);color: #ffffff;"
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--    分页 @pagination="getList"-->
      <el-row>
        <el-col :span="24" :xs="24">
          <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.page"
              :limit.sync="queryParams.limit"
          />
        </el-col>
      </el-row>
<!--      新增-->
      <div class="add">
        <el-dialog :title="title" :visible.sync="open" width="600px" :show-close="false">
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-row>
              <el-col :span="24">
                <el-form-item label="站点名称" prop="name">
                  <el-input v-model="form.name" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="经度" prop="longitude">
                  <el-input v-model="form.longitude" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="纬度" prop="latitude">
                  <el-input v-model="form.latitude" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="设备地址" prop="address">
                  <el-input v-model="form.address" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm" style="background: #00B8B8;color: white;border: none;">确 定</el-button>
            <el-button @click="dialogCancel" style="background: #006675;color: white;border: none;">取 消</el-button>
          </div>
        </el-dialog>
      </div>
<!--      编辑-->
      <div class="add">
        <el-dialog :title="title1" :visible.sync="open1" width="600px" :show-close="false">
          <el-form ref="form" :model="form1" :rules="rules1" label-width="100px">
            <el-row>
              <el-col :span="24">
                <el-form-item label="站点名称" prop="name">
                  <el-input v-model="form1.name" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="经度" prop="longitude">
                  <el-input v-model="form1.longitude" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="纬度" prop="latitude">
                  <el-input v-model="form1.latitude" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="24">
                <el-form-item label="设备地址" prop="address">
                  <el-input v-model="form1.address" placeholder="请输入" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm1" style="background: #00B8B8;color: white;border: none;">确 定</el-button>
            <el-button @click="dialogCancel1" style="background: #006675;color: white;border: none;">取 消</el-button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { getSiteByPage, saveSiteId, getSiteInfo, deleteSiteById } from '@/api/automation'
import Search from '@/components/PageSearch'
import Pagination from "@/components/Pagination";
import RightToolbar from '@/components/RightToolbar'
const defaultForm = {
  name: '',
  longitude: '',
  latitude: '',
  address: ''
}
export default {
  name: "pump",
  components: { Search, Pagination, RightToolbar },
  data() {
    return {
      // 搜索
      searchData: [
        {
          type: 'input',
          label: '站点名称',
          prop: 'name',
          placeholder: '请输入站点名称筛选'
        },
        {
          type: 'input',
          label: '设备地址',
          prop: 'address',
          placeholder: '请输入设备地址筛选'
        }
      ],
      // 分页总条数
      total: 0,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 20,
        name: '',
        address: ''
      },
      // 表格
      tableData: [],
      // 显示搜索条件
      showSearch: true,
      // 列信息
      columns: [
        { key: 0, label: `站点名称`, visible: true },
        { key: 1, label: `经度`, visible: true },
        { key: 2, label: `纬度`, visible: true },
        { key: 3, label: `设备地址`, visible: true },
        { key: 4, label: `创建时间`, visible: true },
        { key: 5, label: `更新时间`, visible: true }
      ],
      // 弹出层标题
      title: "新增站点",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: Object.assign({}, defaultForm),
      // 表单校验
      rules: {
        name: [
          { required: true, message: '请输入站点名称', trigger: 'blur' }
        ]
      },
      id: '',
      title1: "编辑站点信息",
      open1: false,
      form1: {
        id: '',
        name: '',
        longitude: '',
        latitude: '',
        address: ''
      },
      rules1: {
        name: [
          { required: true, message: '请输入站点名称', trigger: 'blur' }
        ]
      },
    }
  },
  props: {},
  watch: {},
  created() {
    this.getList()
  },
  mounted() {
  },
  methods: {
    getList() {
      getSiteByPage(this.queryParams).then((res) => {
        console.log(res)
        this.total = res.dataCounts
        this.tableData = res.data || []
      }).catch((err) => {
        console.log(err)
        this.$message.error(err.errorMsg)
      })
    },
    // 新增
    handleAdd() {
      this.open = true
    },
    dialogCancel() {
      this.open = false
      this.form = Object.assign({}, defaultForm)
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          saveSiteId(this.form).then((res) => {
            this.$message.success('添加成功！')
            this.dialogCancel()
            this.getList()
          }).catch((err) => {
            this.$message.error(err.errorMsg)
          })
        } else {
          this.$message.warning('请填写相关字段内容')
          return false
        }
      })
    },
    // 编辑
    handleEdit(index, row) {
      console.log(index,row)
      this.id = row.id
      getSiteInfo(row.id).then(res => {
        this.form1 = Object.assign({}, res.data)
      }).catch(err => {
        this.$message.error(err.errorMsg)
      })
      // this.form1.name = row.name
      // this.form1.latitude = row.latitude
      // this.form1.longitude = row.longitude
      // this.form1.address = row.address
      this.open1 = true
    },
    dialogCancel1() {
      this.open1 = false
    },
    // 编辑
    submitForm1() {
      const data = {
        id: this.id,
        name: this.form1.name,
        latitude: this.form1.latitude ,
        longitude: this.form1.longitude,
        address: this.form1.address
      }
      saveSiteId(data).then((res) => {
        this.$message.success('编辑成功！')
        this.open1 = false
        this.getList()
      }).catch((err) => {
        this.$message.error(err.errorMsg)
      })
    },
    // 删除
    handleDelete(index,row) {
      console.log(index,row)
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          console.log(action)
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '执行中...'
            deleteSiteById(row.id).then(res => {
              done()
              instance.confirmButtonLoading = false
            }).catch(err => {
              this.$message.error(err.errorMsg)
            })
          } else {
            done()
            instance.confirmButtonLoading = false
          }
        }
      }).then(() => {
        this.$message.success('删除成功!')
        this.getList()
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    // 新的搜索内容
    searchParamsUpdate() {
      this.getList()
    }
  }
}
</script>

<style lang='less' scoped>
.app-container {
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  background: #0C323A;
  .container {
    width: 100%;
    height: 100%;
    border-radius: 10px;
    //padding: 10px;
    background: #002127;
    .title {
      height: 42px;
      line-height: 42px;
      border-bottom: 1px solid #999999;
      color: #ffffff;
      font-size: 14px;
      margin-bottom: 15px;
      padding-left: 20px;
      margin-top: 10px;
    }
    // 显隐列
    .el-col-1 {
      float: right;
    }
  }
}
.add {
  ::v-deep(.el-dialog){
    background: rgba(4,28,33,0.8);
    box-shadow: 0px 0px 20px 0px rgba(0,231,231,0.5);
    border-radius: 20px;
    border: 1px solid rgba(0,231,231,0.698);
    //标题颜色
    .el-dialog__title {
      color: white;
    }
    // 输入框label字体颜色
    .el-form-item__label {
      color: white;
    }
    // 输入框
    .el-input__inner {
      background: #041C21;
      border-radius: 4px 4px 4px 4px;
      border: 1px solid rgba(0,231,231,0.502);
      color: white;
    }
    // 错误规则提示
    .el-form-item__label:before {
      color: #00E7E7;
    }
    // 必填星号
    .el-form-item__error {
      color: #00E7E7;
    }
  }
}
// 表格设置鼠标经过时背景颜色
::v-deep.el-table tbody tr:hover>td{
  background-color: #02565c !important;
}
//表格改变每行底边框颜色
::v-deep.el-table  td, .el-table th.is-leaf  {
  border-bottom:  1px solid rgba(0,231,231,0.302)!important;
}
/*table边框颜色*/
.el-table--border:after,
.el-table--group:after,
.el-table:before {
  background-color: #002127;
}
</style>
